
#include "../stdafx.h"
#include "FlyNode.h"


CFlyNode::CFlyNode()
{
	opentag = false;
	head_over_pos = 0;
	buf1pos = 0;
	buf2pos = 0;
	read_opened = false;
	write_opened = false;
	sel = 0;
	retget  =(GSFlyData *)malloc(sizeof(GSFlyData));
}

void CFlyNode::Close()
{
	if(opentag == true)
	{
		read_opened = false;
		write_opened = false;
		opentag = false;
		fp.Close();
	}
}
CFlyNode::~CFlyNode()
{
	if(opentag == true)
	{
		fp.Close();
	}
	free(retget);
}

GSFlyData * CFlyNode::GetFlyDataBlock(long serial)
{
	if(read_opened == false)
	{
		return NULL;
	}
	if(serial < 0)
	{
		return NULL;
	}
	long pos = sizeof(GSFlyData)*serial + head_over_pos;
	long seekedv= fp.Seek(pos,CFile::begin  );
	if(seekedv != pos)
	{
		return NULL;
	}
	
	if(fp.Read(retget,sizeof(GSFlyData))!=sizeof(GSFlyData))
	{
		return NULL;
	}
	return retget;
}

bool CFlyNode::PushFlydata(GSFlyData * v)
{
	if(v == NULL)
	{
		return false;
	}
	if(opentag ==false)
	{
		return false;
	}
	if(write_opened == false)
	{
		return false;
	}
	if(sel == 0)
	{
		memcpy(&(buf1[buf1pos]),v,sizeof(GSFlyData));
		buf1pos ++;
		if(buf1pos >=GSSAVENUM)
		{
			sel = 1;
			buf1pos = 0;
			fp.Write(buf1,sizeof(GSFlyData)*GSSAVENUM);
		}
		
	}
	else
	{
		memcpy(&(buf2[buf2pos]),v,sizeof(GSFlyData));
		buf2pos ++;
		if(buf2pos >=GSSAVENUM)
		{
			sel = 0;
			buf2pos = 0;
			fp.Write(buf2,sizeof(GSFlyData)*GSSAVENUM);
		}
		
	}
	return true;
}
bool CFlyNode::OpenForRead(CString filepath)
{
	if(head_over_pos < 100)
	{
		return false;
	}
	if(opentag == true)
	{
		return false;
	}
	if(fp.Open(filepath,CFile::modeRead|CFile::typeBinary )==false)
	{
		return false;
	}
	opentag = true;
	fp.Seek(head_over_pos,CFile::begin);
	read_opened = true;
	return true;
}


bool CFlyNode::OpenForWrite(CString filepath)
{
	if(head_over_pos < 100)
	{
		return false;
	}
	if(opentag == true)
	{
		return false;
	}
	if(fp.Open(filepath,CFile::modeWrite|CFile::typeBinary )==false)
	{
		return false;
	}
	opentag = true;
	fp.Seek(head_over_pos,CFile::begin);
	write_opened = true;
	return true;
}

bool CFlyNode::LoadHead(CString filepath)
{
	
	if(opentag == true)
	{
		return false;
	}
	
	if(filepath.IsEmpty())
	{
		return false;
	}
	CString tmp1 = "c:\\model_tmp";
	CString tmp2 = "c:\\ctlpara_tmp";

	if(fp.Open(filepath,CFile::modeRead|CFile::typeBinary )==false)
	{
		return false;
	}

	long ver = 1;
	CBaseFileOp::ReadInt(&fp,ver);
	if(ver != 1)
	{
		fp.Close();
		return false;
	}


	if(1)
	{
		CFile fpt;
		fpt.Open(tmp1,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary ,NULL);
		long len ;
		char buf[1024];
		CBaseFileOp::ReadInt(&fp,len);
		long timess = len /1024;
		long relic = len %1024;
		for(long i = 0; i < timess ; i ++)
		{
			fp.Read(buf,1024);
			fpt.Write(buf,1024);
		}
		if(relic!=0)
		{
			fp.Read(buf,relic);
			fpt.Write(buf,relic);
		}
		fpt.Close();
		this->used_model.Empty();
		if(this->used_model.LoadHelipara(tmp1)==false)
		{
			fp.Close();
			CFile::Remove(tmp1);
			return false;
		}
	}

	
	if(1)
	{
		CFile fpt;
		fpt.Open(tmp2,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary ,NULL);
		long len ;
		char buf[1024];
		CBaseFileOp::ReadInt(&fp,len);
		long timess = len /1024;
		long relic = len %1024;
		for(long i = 0; i < timess ; i ++)
		{
			fp.Read(buf,1024);
			fpt.Write(buf,1024);
		}
		if(relic!=0)
		{
			fp.Read(buf,relic);
			fpt.Write(buf,relic);
		}
		fpt.Close();
		this->used_controlPara.Empty();
		if(this->used_controlPara.LoadParas(tmp2)==false)
		{
			fp.Close();
			CFile::Remove(tmp2);
			return false;
		}
	}

	
	
	head_over_pos = fp.Seek(0,CFile::current);
	this->fp.Close();
	this->opentag =  false;
	
	CFile::Remove(tmp1);
	CFile::Remove(tmp2);

	return true;
}

bool CFlyNode::Makehead(CModelNode * m ,CCtlParaNode *cp,CString filepath)
{
	if(opentag == true)
	{
		return false;
	}
	this->used_model = *m;
	this->used_controlPara = *cp;

	
	if(filepath.IsEmpty())
	{
		return false;
	}
	CString tmp1 = "c:\\model_tmp";
	CString tmp2 = "c:\\ctlpara_tmp";

	if(this->used_model.SaveHelipara(tmp1)==false)
	{
		return false;
	}
	if(this->used_controlPara.SaveParas(tmp2)==false)
	{
		CFile::Remove(tmp1);
		return false;
	}

	if(fp.Open(filepath,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary )==false)
	{
		CFile::Remove(tmp1);
		CFile::Remove(tmp2);
		return false;
	}
	
	long ver = 1;
	CBaseFileOp::WriteInt(&fp,ver);
	if(1)
	{
		CFile fpt;
		fpt.Open(tmp1,CFile::modeRead|CFile::typeBinary ,NULL);
		long len = fpt.GetLength();
		char buf[1024];
		CBaseFileOp::WriteInt(&fp,len);
		long timess = len /1024;
		long relic = len %1024;
		for(long i = 0; i < timess ; i ++)
		{
			fpt.Read(buf,1024);
			fp.Write(buf,1024);
		}
		if(relic!=0)
		{
			fpt.Read(buf,relic);
			fp.Write(buf,relic);
		}
		fpt.Close();
		CFile::Remove(tmp1);
	}
	
	if(1)
	{
		CFile fpt;
		fpt.Open(tmp2,CFile::modeRead|CFile::typeBinary ,NULL);
		long len = fpt.GetLength();
		char buf[1024];
		CBaseFileOp::WriteInt(&fp,len);
		long timess = len /1024;
		long relic = len %1024;
		for(long i = 0; i < timess ; i ++)
		{
			fpt.Read(buf,1024);
			fp.Write(buf,1024);
		}
		if(relic!=0)
		{
			fpt.Read(buf,relic);
			fp.Write(buf,relic);
		}
		fpt.Close();
		CFile::Remove(tmp2);
	}
	
	head_over_pos = fp.Seek(0,CFile::current);
	this->fp.Close();
	this->opentag = false;
	return true;
}
